# Script to extract ani files into JSON
# Based on libs/graphics/gfxload.c

import os, glob, json

basepath = "/usr/share/games/uqm/content/"
jsonpath = "../data/framedata.json"

data = {}
for (dirpath, dirnames, filenames) in os.walk(basepath):
	for filename in filenames:
		if not filename.endswith("lst"):
			continue
		# seems like frame lists are kept in files whose second record in the .lst file is 2.
		# should find out where in the code this flag is.
		lstpath = os.path.join(dirpath, filename)
		for fitem in open(lstpath):
			if not fitem.split(): continue
			_, _, ftype, fname = fitem.split()
			if ftype != "2": continue
			fpath = os.path.join(basepath, fname)
			print lstpath, fpath, fname, dirpath
			fid = fpath[len(basepath):]
			data[fid] = []
			for line in open(fpath):
				pngname, bcolor, cindex, hx, hy = line.split()
				bcolor, cindex, hx, hy = map(int, (bcolor, cindex, hx, hy))
				data[fid].append([pngname, bcolor, cindex, hx, hy])

json.dump(data, open(jsonpath, "w"))

